﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Foothill.Utils;
using GGFW.Entity.DocumentsManage;
using GGFW.Utils.Constants;
using GGFW.DataAccess.DocumentsManage;
using GGFW.DataAccess.Constants;

namespace GGFW.BusinessLogic.DocumentsManage
{
    public class ClientControl
    {
        /// <summary>
        /// 客户信息数据层访问类
        /// </summary>
        private readonly ClientDao dal = new ClientDao();

        #region 查询获取客户信息
        /// <summary>
        /// 根据客户名称获取客户信息列表
        /// </summary>
        /// <param name="queryEntity">查询条件</param>
        /// <param name="context">请求</param>
        /// <returns></returns>
        public FoothillPageData<client> searchClientInfo(int pageSize, int pageNo, string client_name)
        {
            FoothillPageData<client> result = new FoothillPageData<client>();

            try
            {   //成功
                int totalCount = 0;
                List<client> models = Search_NakederClientInfo(pageSize, pageNo, client_name, out totalCount);

                result.Records = (models ?? new List<client>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 根据批次任务名称进行模糊查找
        /// </summary>
        /// <param name="taskname"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public List<client> Search_NakederClientInfo(int pageSize, int pageNo, string client_name, out int totalCount)
        {
            return dal.GetClientInfo(pageSize, pageNo, client_name, out totalCount);
        }
        #endregion

        #region 获取客户列表
        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="queryEntity"></param>
        /// <returns></returns>
        public FoothillPageData<client> GetClientList(int PageSize, int PageIndex, client queryEntity)
        {
            FoothillPageData<client> result = new FoothillPageData<client>();

            try
            {   //成功
                int totalCount = 0;
                List<client> models = GetClientList(PageSize, PageIndex, queryEntity, out totalCount);

                result.Records = (models ?? new List<client>());
                result.totalCount = totalCount;
                result.Success = true;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        /// <summary>
        /// 获取客户列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="queryEntity"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public List<client> GetClientList(int PageSize, int PageIndex, client queryEntity, out int totalCount)
        {
            if (queryEntity == null)
            {
                totalCount = 0;
                return null;
            }
            return dal.GetClientList(PageSize, PageIndex, queryEntity, out totalCount);
        }
        #endregion

        #region 保存客户信息
        /// <summary>
        /// 保存客户信息
        /// </summary>
        /// <param name="clientFileds"></param>
        /// <returns></returns>
        public int SaveClient(string clientFileds)
        {
            int client_id;
            SqlTransaction trans = null;
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(SQLConfig.FoothillDataBaseConnectionString);
                conn.Open();
                trans = conn.BeginTransaction();
                //成功             
                client_id = dal.SaveClient(clientFileds); //新增任务
            }
            catch (Exception ex)
            {
                client_id = 0;
                if (trans != null)
                {
                    trans.Rollback();
                }
            }
            finally
            {
                trans = null;
                if (conn != null && !ConnectionState.Closed.Equals(conn.State))
                {
                    conn.Close();
                }
            }

            return client_id;
        }
        #endregion

        #region 更新客户信息
        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="ClientID"></param>
        /// <param name="FiledName"></param>
        /// <param name="FiledValue"></param>
        /// <returns></returns>
        public FoothillOperationResult<bool> UpdateClientInfo(string ClientID, string FiledName, string FiledValue)
        {
            FoothillOperationResult<bool> result = new FoothillOperationResult<bool>();

            try
            {
                //成功
                result.Success = dal.UpdateClientInfo(ClientID, FiledName, FiledValue); ;
            }
            catch (Exception ex)
            {
                //失败
                GGFW.Utils.Constants.CommonUtil.SealExceptionToFoothillResult(result, ex);
            }

            return result;
        }

        #endregion

        #region 删除客户
        /// <summary>
        /// 删除客户
        /// </summary>
        /// <param name="client_id"></param>
        /// <returns></returns>
        public FoothillOperationResult<client> DelClientByID(int client_id)
        {
            FoothillOperationResult<client> result = new FoothillOperationResult<client>();

            try
            {
                dal.DelClientByID(client_id);
                result.success = true;
            }
            catch
            {
                result.success = false;
            }

            return result;
        }
        #endregion
    }
}
